package com.ssyt.hiring.common;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.common.util.Util;
import com.ssyt.hiring.company.entity.CompanyEntity;
import com.ssyt.hiring.talent.entity.TalentEntity;

public class Constants {

	private Constants() throws IllegalAccessException {
		throw new IllegalAccessException();
	}

	public static final String ENCODE_UTF8 = "UTF-8";
	public static final String HIRING_TALENT = "talent";
	public static final String HIRING_COMPANY = "company";
	public static final int DEFAULT_PAGE_NO = Integer.parseInt(Util.rb
			.getString("page.pageno"));
	public static final int DEFAULT_PAGE_SIZE = Integer.parseInt(Util.rb
			.getString("page.pagesize"));

	/**
	 * 获取放在Session里的Comapny对象
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static CompanyEntity getCompany(HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute("company") == null) {
			request.setAttribute("msg", "会话已关闭！");
			try {
				request.getRequestDispatcher(
						"/WEB-INF/views/company/company-login.jsp").forward(
						request, response);
			} catch (ServletException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			throw new RuntimeException("会话已关闭！获取Session失败！");
		} else {
			return (CompanyEntity) request.getSession().getAttribute("company");
		}
	}

	/**
	 * 获取放在Session里的Comapny的ID
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static int getCompanyId(HttpServletRequest request,
			HttpServletResponse response) {
		return getCompany(request, response).getId();

	}

	/**
	 * 获取放在Session里的Talent对象
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static TalentEntity getTalent(HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute("talent") == null) {
			request.setAttribute("msg", "会话已关闭！");
			try {
				request.getRequestDispatcher(
						"/WEB-INF/views/talent/talent-login.jsp").forward(
						request, response);
			} catch (ServletException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			throw new RuntimeException("会话已关闭！获取Session失败！");
		} else {
			return (TalentEntity) request.getSession().getAttribute("talent");
		}
	}

	/**
	 * 获取放在Session里的Talent的Id
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static int getTalentId(HttpServletRequest request,
			HttpServletResponse response) {
		return getTalent(request, response).getId();

	}

	/**
	 * 判断用户是否登陆
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isTalentLogin(HttpServletRequest request) {
		if (request.getSession().getAttribute("talent") == null) {
			return false;
		}
		return true;
	}

	/**
	 * 判断用户是否登陆
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isCompanyLogin(HttpServletRequest request) {
		if (request.getSession().getAttribute("company") == null) {
			return false;
		}
		return true;
	}
}
